﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Active.Core;

public partial class Finance_FinanceStatistics : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnCalculate_Click(object sender, EventArgs e)
    {
        this.lblError.Text = string.Empty;
        switch (ddlSearchType.SelectedValue)
        {
            case "Agent":
                try
                {
                    Agent agent = Agent.GetAgent(txtAgentName.Text);
                    Calculate(agent.Id, 0, 0);
                }
                catch
                {
                    this.lblError.Text = "代理名有误！";
                }
                break;
            case "Customer":
                try
                {
                    Customer cust = Customer.GetCustomer(txtCustomerName.Text);
                    Calculate(0, cust.Id, 0);
                }
                catch
                {
                    this.lblError.Text = "客户名有误！";
                }
                break;
            default:
                try
                {
                    Media media = Media.GetMedia(txtMediaName.Text);
                    Calculate(0, 0, media.Id);
                }
                catch
                {
                    this.lblError.Text = "媒介名有误！";
                }
                break;
        }
    }

    private void Calculate(int agentId, int custId, int mediaId)
    {
        DateTime start = DateTime.Parse(txtStartDate.Text);
        DateTime end = DateTime.Parse(txtEndDate.Text);
        StatisticsInfo info;

        if (mediaId == 0)
        {
            info = FinanceManager.GetStatisticsInfo(agentId, custId, start, end);
        }
        else
        {
            info = FinanceManager.GetStatisticsInfo(mediaId, start, end);
        }
        this.lblGain.Text = info.Gain.ToString("c2");
        this.lblRealGain.Text = info.RealGain.ToString("c2");
        this.lblTotalIncome.Text = info.Income.ToString("c2");
        this.lblTotalPayout.Text = info.Payout.ToString("c2");
        this.lblTotalRealIncome.Text = info.RealIncome.ToString("c2");
        this.lblTotalRealPayout.Text = info.RealPayout.ToString("c2");
        this.lblTotalCount.Text = info.TotalCount.ToString();

        plResult.Visible = true;
        BillsGridView.Visible =
        OrderListGridView.Visible = false;
        if (mediaId == 0)
        {
            BillsGridView.DataSource = info.Records;
            BillsGridView.DataBind();
            BillsGridView.Visible = true;
        }
        else
        {
            OrderListGridView.DataSource = info.Records;
            OrderListGridView.DataBind();
            OrderListGridView.Visible = true;
        }
    }
}
